#ifndef DOWNLOADTHREAD_H
#define DOWNLOADTHREAD_H

#include <QThread>

class YDDownloadManager;

class YDDownloadThread : public QThread {
    Q_OBJECT
public:
    explicit YDDownloadThread(QObject *parent = 0);
    ~YDDownloadThread();

    virtual void run();

    void startDownload(const QString& url, const QString& filePath, bool append);
    void stopDownload(const QString& url);

signals:
    void sigStarted(const QString& url);
    void sigStoped(const QString& url);
    void sigErrorOccured(const QString& url, int code);
    void sigFinished(const QString& url);
    void sigProgressChanged(const QString& url, qint64 bytesReceived, qint64 bytesTotal);
    void sigDuplicate(const QString& url);

    void sigStartDownload(const QString& url, const QString& filePath, bool append);
    void sigStopDownload(const QString& url);

private slots:
    // 这些 slot 位于主线程，所以他们会被子线程的 signal 异步调用
    void started(const QString& url);
    void stoped(const QString& url);
    void errorOccured(const QString& url, int code);
    void finished(const QString& url);
    void progressChanged(const QString& url, qint64 bytesReceived, qint64 bytesTotal);
    void duplicate(const QString& url);

private:
    YDDownloadManager* mgr;
};

#endif // DOWNLOADTHREAD_H
